package com.ywk.base.concurrency.lock;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * CAS无锁优化
 * @Author zbh
 * @Date 2023/2/2
 */
public class CasTest {
    private AtomicInteger count = new AtomicInteger();

    //同一个时间可以有多个线程进入add方法
    public void add(){
        //底层通过CAS实现线程安全
        count.getAndIncrement();
    }

    public static void main(String[] args) {
        CasTest casTest = new CasTest();
        //多个线程都基于casTest对象来调用add方法
        casTest.add();
        System.out.println(casTest.count.get());

        ReentrantLock reentrantLock = new ReentrantLock();
    }

}
